package com.lx.boot.web.convert.deserializer;

import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.lx.annotation.Note;
import com.lx.boot.web.convert.DFunction;
import com.lx.boot.web.convert.deserializer.DecoderDeserialize.DecoderDFunction;
import com.lx.util.secure.algorithm.coder.CoderType;

import java.lang.annotation.*;

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD,ElementType.TYPE,ElementType.PARAMETER})
@Documented
@JacksonAnnotationsInside
@Deserialize(value = DecoderDeserialize.class,function = DecoderDFunction.class)
public @interface DecoderDeserialize {

    @Note("指定解码类型")
    CoderType value();


    public static  class DecoderDFunction implements DFunction<DeserializerPara, DecoderDeserialize, Object> {
        @Override
        public Object apply(DeserializerPara deserializerPara, DecoderDeserialize convert) {
            Object value = deserializerPara.getValue();
            if (value == null){
                return null;
            }
            Class filedType = deserializerPara.getFiledType();
            if (String.class.isAssignableFrom(filedType)){
                return convert.value().decodeToString((String) value);
            }else if (byte[].class.isAssignableFrom(filedType)){
                return convert.value().decode((byte[]) value);
            }
            throw new RuntimeException("请检查待解码/解密的参数类型是否为String 或 byte[]");
        }
    }


}
